package hc.uco

class MedicacaoController  extends BaseController {

	def beforeInterceptor = [action:this.&auth ]
	
    static allowedMethods = [save: "POST", update: "POST", delete: "GET"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
		params.sort = "principioAtivo"
		params.order = "asc"
        [medicacaoInstanceList: Medicacao.list(params), medicacaoInstanceTotal: Medicacao.count()]
    }

    def create = {
        def medicacaoInstance = new Medicacao()
        medicacaoInstance.properties = params
        return [medicacaoInstance: medicacaoInstance]
    }

    def save = {
        def medicacaoInstance = new Medicacao(params)
		medicacaoInstance.createdBy = session.usuario;
		medicacaoInstance.updatedBy = medicacaoInstance.createdBy;
        if (medicacaoInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'medicacao.label', default: 'Medicacao'), medicacaoInstance])}"
            //redirect(action: "show", id: medicacaoInstance.id)
			redirect(action: "manage", id: medicacaoInstance.paciente.id)
        }else {
            //render(view: "create", model: [medicacaoInstance: medicacaoInstance])
			chain(action: "manage", id: medicacaoInstance.paciente.id, model: [medicacaoInstance : medicacaoInstance])
        }
    }

    def show = {
        def medicacaoInstance = Medicacao.get(params.id)
        if (!medicacaoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'medicacao.label', default: 'Medicacao'), params.id])}"
            redirect(action: "list")
        }
        else {
            [medicacaoInstance: medicacaoInstance]
        }
    }

    def edit = {
        def medicacaoInstance = Medicacao.get(params.id)
        if (!medicacaoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'medicacao.label', default: 'Medicacao'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [medicacaoInstance: medicacaoInstance]
        }
    }

    def update = {
        def medicacaoInstance = Medicacao.get(params.id)
        if (medicacaoInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (medicacaoInstance.version > version) {
                    
                    medicacaoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'medicacao.label', default: 'Medicacao')] as Object[], "Another user has updated this Medicacao while you were editing")
                    render(view: "edit", model: [medicacaoInstance: medicacaoInstance])
                    return
                }
            }
            medicacaoInstance.properties = params
            if (!medicacaoInstance.hasErrors() && medicacaoInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'medicacao.label', default: 'Medicacao'), medicacaoInstance.id])}"
                redirect(action: "show", id: medicacaoInstance.id)
            }
            else {
                render(view: "edit", model: [medicacaoInstance: medicacaoInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'medicacao.label', default: 'Medicacao'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def medicacaoInstance = Medicacao.get(params.id)
        if (medicacaoInstance) {
            try {
                medicacaoInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'medicacao.label', default: 'Medicacao'), medicacaoInstance])}"
                //redirect(action: "list")
				redirect(action: "manage", id: medicacaoInstance.paciente.id)
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'medicacao.label', default: 'Medicacao'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'medicacao.label', default: 'Medicacao'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def manage = {
		
		def medicacao = new Medicacao()
		medicacao.properties = params
		
		if (request.method == 'GET') {
			def paciente = Paciente.get(params.id)
			params['paciente.id'] = params.id
			params.sort = "principioAtivo"
			params.order = "asc"
			def medicacaoInstanceList = Medicacao.findAll("from Medicacao as m where m.paciente=:pac and m.tipo='dom' order by m.principioAtivo asc", [pac:paciente])
			//findAllByPacienteAndTipo(paciente, 'dom')
			def medicacao24hInstanceList = Medicacao.findAll("from Medicacao as m where m.paciente=:pac and m.tipo='24h' order by m.principioAtivo asc", [pac:paciente])
			//findAllByPacienteAndTipo(paciente, '24h')
			return ['pacienteInstance':paciente, medicacaoInstanceList: medicacaoInstanceList, medicacao24hInstanceList: medicacao24hInstanceList ]
		} else {
			medicacao.save()
		}
	}
}
